package ph.com.gs3.formalistics.presenter.fragments.views;

import java.util.List;

import ph.com.gs3.formalistics.R;
import ph.com.gs3.formalistics.model.valueobjects.business.User;
import ph.com.gs3.formalistics.presenter.adapters.UserListItemViewAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class OnlineUsersViewFragment extends Fragment {

	public static final String TITLE = "Online Users";

	private ListView lvOnlineUsersList;
	private UserListItemViewAdapter onlineUsersListAdapter;

	private OnlineUsersViewActionListener actionListener;

	public static OnlineUsersViewFragment createInstance() {
		OnlineUsersViewFragment instance = new OnlineUsersViewFragment();

		return instance;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		try {
			actionListener = (OnlineUsersViewActionListener) activity;
		} catch (ClassCastException e) {
			throw new IllegalStateException(activity.getClass().getSimpleName()
			        + " must implement OnlineUsersViewActionListener");
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_online_users, container, false);

		// onlineUsersListAdapter = new UserListItemViewAdapter(getActivity(),
		// DummyUsers.getDummyUsers());
		onlineUsersListAdapter = new UserListItemViewAdapter(getActivity(), null);
		lvOnlineUsersList = (ListView) rootView.findViewById(R.id.OnlineUsers_llOnlineUserList);
		lvOnlineUsersList.setAdapter(onlineUsersListAdapter);

		lvOnlineUsersList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

				User user = (User) onlineUsersListAdapter.getItem(position);
				actionListener.onUserClicked(user);

			}

		});

		return rootView;
	}

	public void setDisplayingUsers(List<User> users) {
		onlineUsersListAdapter.resetItems(users);
	}

	public static interface OnlineUsersViewActionListener {

		public void onUserClicked(User user);

	}

}
